package blue.webserver;

import java.net.InetSocketAddress;
import com.sun.net.httpserver.HttpServer;

import blue.logger.Subscriber;
import blue.platform.ServerInterface;

public class WebServer implements ServerInterface{
	private HttpServer server_;
	/* Connect  */
	private ZmqPublisher master_pub_;
	private ZmqSubscriber master_sub_;
	/* Subscribe message about command list from datacenter2. */
	private Subscriber commandlist_sub_;
	
	/* Start web-server.
	 * Args:
	 * 		web_port: port for web-server.
	 * 		master_port: port for master-server, it will connect to master-server
	 *  				as a slave.
	 *  	master_pub_port: port used to publish message to master.
	 *  	master_sub_port: port used to subscribe message from master.
	 * 		sub_port: port for subscribe message from datacenter2. (Actually
	 * 					command list).
	 */
	public WebServer(String web_port, String master_pub_port, String master_sub_port
			, String master_command_port) throws Exception
	{
		// Connect to master_server
		master_pub_ = new ZmqPublisher(master_pub_port);
		master_sub_ = new ZmqSubscriber(master_sub_port, "");
		
		commandlist_sub_ = new Subscriber("localhost",master_command_port,"");

		// Bind web address to Port.
        InetSocketAddress web_addr = new InetSocketAddress(Integer.parseInt(web_port));
        String web_ip = web_addr.getAddress().getHostAddress();
        System.out.println("WebServer binded to "+web_ip+":"+web_port);
	    server_ = HttpServer.create(web_addr, 0);

	    // Build URL map-table.
        String curr_dir = System.getProperty("user.dir");
        System.out.println("Run Root Directory is "+curr_dir);
        /* Gradle path */
//        String page_home_path = curr_dir + "/resources/main/index.html";
//        String page_cl_path = curr_dir + "/resources/main/commandlist.html";// command list page
//        String page_chat_path = curr_dir + "/resources/main/chat.html";// command list page
//        String page_problems_path = curr_dir + "/resources/main/problems.html";
//        String client_path = curr_dir + "/resources/main/client/client.py";
//        String static_dir = curr_dir+"/resources/main";
        /* Eclipse path */
        String page_home_path = curr_dir + "/src/main/resources/index.html";
        String page_cl_path = curr_dir + "/src/main/resources/commandlist.html";// command list page
        String page_chat_path = curr_dir + "/src/main/resources/chat.html";// command list page
        String page_problems_path = curr_dir + "/src/main/resources/problems.html";
        String client_path = curr_dir + "/src/main/resources/client/client.py";
        String static_dir = curr_dir+"/src/main/resources";
        
        server_.createContext("/", new HomeHandler(page_home_path));
        server_.createContext("/index.html", new HomeHandler(page_home_path));
        server_.createContext("/commandlist.html", new HomeHandler(page_cl_path));
        server_.createContext("/chat.html", new HomeHandler(page_chat_path));
        server_.createContext("/problems.html", new HomeHandler(page_problems_path));
        server_.createContext("/getclient", new ClientDownloadHandler(client_path));
        server_.createContext("/assets_m", new FileHandler(static_dir));
        server_.createContext("/chat/message", new ChatMessageHandler(master_pub_,master_sub_));
        server_.createContext("/commandlist/item", new CommandListHandler(commandlist_sub_));
	    // Others
	    server_.setExecutor(null); // creates a default executor
	}
	
	//@override
	public void start_server(){
		server_.start();
	}
}
